﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

namespace CSE445_Assignment2_ChickenSales
{
    class cRetailer
    {
       
        public void retailerFunc()
        {   //for starting thread
             Random rand = new Random();
            cChickenFarm chicken = new cChickenFarm();
           
          int x = Convert.ToInt32(Thread.CurrentThread.Name);
            for (Int32 i = 0; i < 10; i++)
            {
               
                Thread.Sleep(500 + rand.Next(1,1000));
                Int32 p = chicken.getPrice();
                //Console.WriteLine("Store{0} has everyday low price: ${1} each", Thread.CurrentThread.Name, p);
                buyChickens(x, p);
            }
            
        }
  
        public void buyChickens(Int32 myNum, Int32 p)
        {
            cChickenFarm chicken = new cChickenFarm();
            Random rand = new Random();
            int numOchikns = rand.Next(1, 100);
           // Console.WriteLine("Store{0} chickens are on sale: as low as ${1} each", Thread.CurrentThread.Name, p);
            string x = "";

            if (p < 7)
            {
                //Threads 4-7 will buy
                //if (Thread.CurrentThread.Name == "4" || Thread.CurrentThread.Name == "5" || Thread.CurrentThread.Name == "6" || Thread.CurrentThread.Name == "7")
                if (myNum == 4 || myNum == 5 || myNum == 6 || myNum == 7)
                {
                  //  Console.WriteLine("\"Store {0}\" buys {1} chickens at ${2} each", Thread.CurrentThread.Name,numOchikns , p);
                    x = makeOrderStr(myNum, numOchikns * myNum, p);
                    placeOrder(x);
                }

            }
            else if (p < 4)
            {
                //Threads 0-9 buys
               // Console.WriteLine("\"Store {0}\" buys {1} chickens at ${2} each", Thread.CurrentThread.Name, numOchikns, p);
                x = makeOrderStr(myNum, numOchikns * myNum, p);
                placeOrder(x);
            }
            else
            {
                //price too high. Threads 0-3 is always buying though.
                //
                if (myNum == 0 || myNum == 1 || myNum == 2 || myNum == 3)
                {
                   // Console.WriteLine("\"Store {0}\" buys {1} chickens at ${2} each", Thread.CurrentThread.Name, numOchikns, p);
                    x = makeOrderStr(myNum, numOchikns * myNum, p);
                    placeOrder(x);
                }
              
            }
           
        }
        public void chickenOnSale(Int32 p)
        {  // Event handler

            Console.WriteLine("Chickens price dropped! Now ${0} apiece!" , p);
        }

        private string makeOrderStr(int myNum, int nToBuy, int price)
        {

            String sOrder = "";
            cOrder myOrder = new cOrder();
           
            cDecEnc myEnc = new cDecEnc();
            int myID = myNum;
            myOrder.senderID = myID;
            myOrder.amount = nToBuy;
            myOrder.cardNo = 111111 + myID;
            myOrder.price = price;
            myOrder.date = DateTime.Now;
            sOrder = myEnc.Encode(myOrder);
            return sOrder;
        }
        
        private void placeOrder(string order)
        {
           // cBuffer buffout = new cBuffer();
            //buffout.orderEr(order);
            myApplication myne = new myApplication();
            Thread orderThread = new Thread(myne.getInLine);
            orderThread.Start(order);
           // myne.getInLine(order);

        }
       public void saleDone(Int32 id, Int32 numSold)
        {
            Console.WriteLine("Retailer {0} bought {1} chickens!",id,numSold);
        }

    }
}
